# -*- coding: utf-8 -*
import paramiko
import os

class SSHcient():
    def __init__(self,username='gdlocal',password='gdlocal',hostname='169.254.1.10',port=22):
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
    def remote_command(self,command):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname=self.hostname,port=self.port,username=self.username,password=self.password)
        stdin,stdout,stderr = client.exec_command(command)
        print(stdout.read().decode('utf-8'))
        client.close()
    def upload_file(self,localPath,remotePath):
        transport = paramiko.Transport((self.hostname, self.port))
        transport.connect(username=self.username, password=self.password)
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.put(localPath,remotePath)
        transport.close()
    def zip_file(self,file_path,zip_file_path):
        os.popen("ditto -ck --keepParent "+file_path+' '+zip_file_path)

if __name__ == '__main__':
    hostname = '172.20.10.2'
    port = 22
    username = 'kk'
    password = '147258-Poi'
    path = '/Users/answer.guo/Desktop/work'
    localPath = '/Users/answer.guo/Desktop/work.zip'
    
    remotePath = '/Users/kk/Desktop/data/work.zip'
    remote = SSHcient(username,password,hostname,port)
    remote.zip_file(path,localPath)
    remote.remote_command('whoami')
    remote.upload_file(localPath,remotePath)



